package com.pattern.command;

/**
 * @author 冯志立
 * @create 2019-11-15 9:47
 */
public class RemoteController {
    //开  按钮的命令数组
    Command[] onCommands;
    Command[] offCommands;
    //执行撤销命令
    Command undoCommand;

    public RemoteController() {
         onCommands = new Command[5];
         offCommands = new Command[5];
         for (int i = 0; i < 5; i++){
             onCommands[i] = new NoCommand();
             offCommands[i] = new NoCommand();
         }
    }

    //给我们的按钮设置你需要的命令即可
    public void setCommond(int no, Command onCommand, Command offCommand){
        onCommands[no] = onCommand;
        offCommands[no] = offCommand;
    }

    //按下 开 按钮
    public void onButtonWasPushed(int no){
        //找到你按下的开的按钮，并调用对应的方法
        onCommands[no].execute();
        //记录这一次的操作，用于撤销
        undoCommand = onCommands[no];
    }

    //按下关的按钮
    public  void offButtonWasPushed(int no){
        offCommands[no].execute();
        undoCommand = offCommands[no];
    }

    //按下撤销按钮
    public void undoButtonWasPushed(){
        undoCommand.undo();
    }
}
